/**
 * 
 */
package com.swim.natatoriuminfo.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.swim.natatoriuminfo.entity.CoachMien;
import com.swim.natatoriuminfo.service.CoachMienService;

/**
 * 教练风采Controller
 * @author ljw
 * @version 2022-08-15
 */
@Controller
@RequestMapping(value = "${adminPath}/natatoriuminfo/coachMien")
public class CoachMienController extends BaseController {

	@Autowired
	private CoachMienService coachMienService;
	
	@Value("${server.context-path}")
	private String contextPath;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public CoachMien get(String id, boolean isNewRecord) {
		return coachMienService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("natatoriuminfo:coachMien:view")
	@RequestMapping(value = {"list", ""})
	public String list(CoachMien coachMien, Model model) {
		model.addAttribute("coachMien", coachMien);
		return "swim/natatoriuminfo/coachMienList";
	}
	
	/**
	 * 查询列表数据
	 */
//	@RequiresPermissions("natatoriuminfo:coachMien:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<CoachMien> listData(CoachMien coachMien, HttpServletRequest request, HttpServletResponse response) {
		Page<CoachMien> pageList = coachMienService.findPage(new Page<CoachMien>(request, response), coachMien); 
		List<CoachMien> coachMienList = pageList.getList();
		if (CollectionUtils.isNotEmpty(coachMienList)) {
			for (CoachMien coachMienOne : coachMienList) {
				//获取教练图片
				List<FileUpload> fileUploadListOne = FileUploadUtils.findFileUpload(coachMienOne.getId(), "coachMien_image");
				if (!CollectionUtils.isEmpty(fileUploadListOne)) {
					coachMienOne.setCoachImageUrl(contextPath + "/userfiles/fileupload/" + fileUploadListOne.get(0).getFileEntity().getFilePath() + fileUploadListOne.get(0).getFileEntity().getFileId() + "." + fileUploadListOne.get(0).getFileEntity().getFileExtension());
				}
				// 标签集合
				coachMienOne.setTagList(Arrays.asList(coachMienOne.getTags().split(",")));
			}
		}
		pageList.setList(coachMienList);
		return pageList;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("natatoriuminfo:coachMien:view")
	@RequestMapping(value = "form")
	public String form(CoachMien coachMien, Model model) {
		model.addAttribute("coachMien", coachMien);
		return "swim/natatoriuminfo/coachMienForm";
	}

	/**
	 * 保存教练风采
	 */
	@RequiresPermissions("natatoriuminfo:coachMien:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated CoachMien coachMien) {
		coachMienService.save(coachMien);
		return renderResult(Global.TRUE, text("保存教练风采成功！"));
	}
	
	/**
	 * 删除教练风采
	 */
	@RequiresPermissions("natatoriuminfo:coachMien:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(CoachMien coachMien) {
		coachMienService.delete(coachMien);
		return renderResult(Global.TRUE, text("删除教练风采成功！"));
	}
	
}